package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.BannerModel;
import com.hangzhou.xyj.ui.activity.WebActivity;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DT-038 on 2015/11/3.
 * 首页惊叫9点半轮播Adapter
 */
public class BannerAdapter extends PagerAdapter {

    private final String TAG = BannerAdapter.class.getSimpleName();

    private Context mContext;
    private List<MyImageView> mViewList = new ArrayList<>();
    private List<BannerModel> mData;

    public BannerAdapter(Context context, List<BannerModel> data) {
        this.mContext = context;
        this.mData = data;
    }

    public void update(List<BannerModel> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mData == null || mData.isEmpty()) {
            return 0;
        }
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    /**
     * 最多3个ItemView：缺点：3个以下增加内存，3个以上减少内存
     *
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        MyImageView itemView;
        final int viewPosition = position % mData.size();
        if (mViewList.size() <= viewPosition) {
            itemView = new MyImageView(mContext);
            mViewList.add(itemView);
            itemView.displayImage(mData.get(viewPosition).getImgUrl());
        } else {
            itemView = mViewList.get(viewPosition);
        }
//        MyImageView itemView;
//        if (mViewList.isEmpty()) {
//            itemView = new MyImageView(mContext);
//            mViewList.add(itemView);
//        }
//
//        final int viewPosition = position % mViewList.size();
//        itemView = mViewList.get(viewPosition);
//        if (itemView.getParent() != null) {
//            itemView = new MyImageView(mContext);
//            mViewList.add(itemView);
//        }
        container.addView(itemView);

        //增加item点击监听
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, WebActivity.class);
                intent.putExtra(DataKey.INDEX, mData.get(viewPosition).getUrl());
                intent.putExtra(DataKey.TITLE, mData.get(viewPosition).getDesc());
                IntentManager.getInstance().goActivity(mContext, intent);
            }
        });

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        //保证只保存3个View
//        if (mViewList.size() > 3) {
//            mViewList.remove(object);
//        }
    }
}
